package com.tedu.openmedical.duplicate.copy_purpose.service.impl;


import com.tedu.openmedical.duplicate.copy_content.mapper.CopyContentMapper;
import com.tedu.openmedical.duplicate.copy_content.pojo.dto.ContentQuery;
import com.tedu.openmedical.duplicate.copy_content.pojo.vo.CopyContentVO;
import com.tedu.openmedical.duplicate.copy_purpose.mapper.CopyPurposeMapper;
import com.tedu.openmedical.duplicate.copy_purpose.pojo.dto.CopyPurposeParam;
import com.tedu.openmedical.duplicate.copy_purpose.pojo.dto.CopyPurposeQuery;
import com.tedu.openmedical.duplicate.copy_purpose.pojo.entity.CopyPurpose;
import com.tedu.openmedical.duplicate.copy_purpose.pojo.vo.CopyPurposeVO;
import com.tedu.openmedical.duplicate.copy_purpose.service.CopyPurposeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CopyPurposeServiceImpl implements CopyPurposeService {
    private static final Logger log = LoggerFactory.getLogger(CopyPurposeServiceImpl.class);
    @Autowired
    private CopyPurposeMapper copyPurposeMapper;
    @Autowired
    private CopyContentMapper copyContentMapper;

    @Override
    public List<CopyPurposeVO> selectTemplate(CopyPurposeQuery query){
        //这里需要处理一下取出的模板数据
        List<CopyPurpose> templates = copyPurposeMapper.selectTemplate(query);
        List<CopyPurposeVO> purposes=new ArrayList<>();
        if(templates!=null){
            for(int i=0;i<templates.size();i++){
                CopyPurpose template=templates.get(i);
                CopyPurposeVO purpose=new CopyPurposeVO();
                BeanUtils.copyProperties(template,purpose);
                //推荐内容转换
                List<Integer> recommend=new ArrayList<>();
                purpose.setRecommendText(transTemp(template.getRecommend(),recommend));
                purpose.setRecommend(recommend);
                purposes.add(purpose);
            }
        }
        return purposes;
    }
    @Override
    public void updateTemplate(CopyPurposeParam param){
        CopyPurpose purpose=new CopyPurpose();
        BeanUtils.copyProperties(param,purpose);
        //根据有无id判断更新还是新增
        if(purpose.getId()==null){
            //新增流程
            purpose.setCreateTime(new Date());
            purpose.setRecommend(transSaveTemp(param.getRecommend()));
            copyPurposeMapper.insertTemplate(purpose);
        }
        else{
            //更新流程
            purpose.setRecommend(transSaveTemp(param.getRecommend()));
            copyPurposeMapper.updateTemplate(purpose);
        }
    }
    @Override
    public void deleteTemplate(Long id){
        copyPurposeMapper.deleteTemplate(id);
    }

    //推荐内容转换&查询
    private String transTemp(String text,List<Integer> list){
        StringBuilder purposes=new StringBuilder();
        String[] strs=text.split(",");
        for(String i:strs){
            //放进推荐内容的查询类里
            ContentQuery query=new ContentQuery();
            int pId=Integer.parseInt(i);
            query.setId(pId);
            //放进推荐内容的数据组里
            list.add(pId);
            List<CopyContentVO> content = copyContentMapper.loadContent(query);
            purposes.append(content.get(0).getName());
            purposes.append(',');
        }
        //删一下多的逗号
        purposes.deleteCharAt(purposes.length()-1);
        return purposes.toString();
    }
    //推荐内容数组转换成存储格式的字符串
    private String transSaveTemp(List<Integer> list){
        StringBuilder saveParam=new StringBuilder();
        for(int i:list){
            saveParam.append(i);
            saveParam.append(",");
        }
        saveParam.deleteCharAt(saveParam.length()-1);
        return saveParam.toString();
    }
}
